package com.miracle.anychatdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;

import com.bairuitech.anychat.AnyChatCoreSDK;
import com.bairuitech.anychat.AnyChatDefine;

import java.util.List;

public class MultiVideoChatAdapter extends RecyclerView.Adapter{
    private final AnyChatCoreSDK mAnychatSDK;
    private List<MultiVideoChatInfo> mInfos;
    private Context mContext;
    private OnMultiVideoChatItemClickListener mOnItemClickListener;


    public MultiVideoChatAdapter(Context activity,List<MultiVideoChatInfo> infos,AnyChatCoreSDK anychatSDK) {
        mInfos = infos;
        mContext =activity;
        mAnychatSDK =anychatSDK;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {			//这里直接return一个holder对象，参数为item的view
        return new MultiVideoChatHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_item_multi_video_chat,null));
    }

    @Override		
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {			//在这部分做对UI的操作
        final MultiVideoChatInfo info = mInfos.get(position);
        SurfaceView surfaceView = ((MultiVideoChatHolder) holder).mSurfaceView;

        if (info.getUserId().equals("-1")){    //初始化本地视频

            // 如果是采用Java视频采集，则需要设置Surface的CallBack
            if (AnyChatCoreSDK
                    .GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA) {
                surfaceView.getHolder().addCallback(AnyChatCoreSDK.mCameraHelper);
            }
            surfaceView.setZOrderOnTop(true);



            // 判断是否显示本地摄像头切换图标
            if (AnyChatCoreSDK
                    .GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA) {
                if (AnyChatCoreSDK.mCameraHelper.GetCameraNumber() > 1) {
                    // 默认打开前置摄像头
                    AnyChatCoreSDK.mCameraHelper
                            .SelectVideoCapture(AnyChatCoreSDK.mCameraHelper.CAMERA_FACING_FRONT);
                }
            } else {
                String[] strVideoCaptures = mAnychatSDK.EnumVideoCapture();
                if (strVideoCaptures != null && strVideoCaptures.length > 1) {
                    // 默认打开前置摄像头
                    for (int i = 0; i < strVideoCaptures.length; i++) {
                        String strDevices = strVideoCaptures[i];
                        if (strDevices.indexOf("Front") >= 0) {
                            mAnychatSDK.SelectVideoCapture(strDevices);
                            break;
                        }
                    }
                }
            }

        }else {
            if (AnyChatCoreSDK
                    .GetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_DRIVERCTRL) == AnyChatDefine.VIDEOSHOW_DRIVER_JAVA) {
                int index = mAnychatSDK.mVideoHelper.bindVideo(surfaceView
                        .getHolder());
                mAnychatSDK.mVideoHelper.SetVideoUser(index, Integer.parseInt(info.getUserId()));
            }
        }



        surfaceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null){
                    mOnItemClickListener.onClick(info);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mInfos.size();
    }

    /**
     *  添加用户视图
     * @param info
     */
    public void insert(MultiVideoChatInfo info){
        info.setPosition(mInfos.size());
        mInfos.add(info);
        notifyItemInserted(mInfos.size());
    }

    /**
     *  移除用户视图
     * @param userId
     */
    public void remove(int userId){

        int pos = -1;
        for (MultiVideoChatInfo info :
                mInfos) {
            if (String.valueOf(userId).equals(info.getUserId())){
                pos = info.getPosition();
            }
        }

        if (pos != -1){
            mInfos.remove(pos);
            notifyItemRemoved(pos);
        }
    }

    /**
     *  打开所有用户的音视频控制
     */
    public void openAllControl(){
        for (MultiVideoChatInfo info :
                mInfos) {
            int  userId = Integer.parseInt(info.getUserId());
            mAnychatSDK.UserCameraControl(userId, 1);
            mAnychatSDK.UserSpeakControl(userId, 1);
        }

    }

    /**
     *  关闭所有用户的音视频控制
     */
    public void closeAllControl(){
        for (MultiVideoChatInfo info :
                mInfos) {
            int  userId = Integer.parseInt(info.getUserId());
            mAnychatSDK.UserCameraControl(userId, 0);
            mAnychatSDK.UserSpeakControl(userId, 0);
        }
    }


    public void setOnItemClickListener(OnMultiVideoChatItemClickListener onItemClickListener){
        mOnItemClickListener = onItemClickListener;
    }

    class MultiVideoChatHolder extends RecyclerView.ViewHolder{	//holder是继承RecycleView的用法基本与gridview相同

        SurfaceView mSurfaceView;

        public MultiVideoChatHolder(View itemView) {
            super(itemView);
            mSurfaceView = itemView.findViewById(R.id.mSurfaceView);
        }

    }

    public interface OnMultiVideoChatItemClickListener {
        void onClick(MultiVideoChatInfo info);
    }
}

